package edu.ycu.aladdin.web.controller.crawler;

import com.alibaba.dubbo.config.annotation.Reference;
import edu.ycu.aladdin.collector.api.WeiboUserInfoService;
import edu.ycu.aladdin.collector.api.entity.weibo.UserInfo;
import edu.ycu.aladdin.web.api.crawler.WeiBoUserInfoAPI;
import edu.ycu.aladdin.web.common.PageData;
import edu.ycu.aladdin.web.common.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/crawler/data/weibo")
public class WeiBoUserInfoController implements WeiBoUserInfoAPI {

    @Reference(check = false, timeout = 10000)
    private WeiboUserInfoService weiboUserInfoService;

    @Override
    public RestResponse addUser(@RequestParam("uid") Long uid,@RequestParam("cover") boolean cover) throws IOException {

        UserInfo userInfo = weiboUserInfoService.addUser(uid,cover);
        log.info("add weibo user id:{}",uid);
        return RestResponse.successResponse(userInfo);
    }

    @Override
    public RestResponse del(@RequestParam("uid") Long uid) {
        long l = weiboUserInfoService.delUser(uid);
        return RestResponse.successResponse(l);
    }

    @Override
    public RestResponse getUserInfo(@RequestParam("uid") Long uid) {
        UserInfo userInfo = weiboUserInfoService.getUserInfo(uid);
        return RestResponse.successResponse(userInfo);
    }


    @Override
    public RestResponse getUserInfos(@RequestParam("page") long page, @RequestParam("num") int num) {
        long total = weiboUserInfoService.userInfoCount();
        List<UserInfo> infoList = weiboUserInfoService.getInfoList(page, num);
        PageData<UserInfo> pageData = new PageData<>(total, page, num, infoList);
        return RestResponse.successResponse(pageData);
    }
}
